package com.mystore.manage.controller;

import com.mystore.framework.pojo.AppModel;
import com.mystore.framework.service.impl.IAppModelServiceImpl;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * 应用模型表(AppModel)表控制层
 */
@Controller
@RequestMapping("manage/appModel")
public class AppModelController {
    /**
     * 服务对象
     */
    @Resource
    private IAppModelServiceImpl iAppModelServiceImpl;

    @GetMapping("/page.do")
    public String page(@RequestParam(defaultValue="") String data,Model model, @RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size,String result) {
        IPage<AppModel> appModelPage = iAppModelServiceImpl.selectPageByData(data,new Page<>(current, size));
        model.addAttribute("appModels", appModelPage);
        model.addAttribute("result",result);
        model.addAttribute("data",data);
        return "appModel_page";
    }

    @GetMapping("/insert.do")
    public String insert(Model model) {
        model.addAttribute("mode", "insert");
        return "appModel_info";
    }

    @GetMapping("/update.do")
    public String update(Model model, int appModelId) {
        model.addAttribute("appModel", iAppModelServiceImpl.selectById(appModelId));
        model.addAttribute("mode", "update");
        return "appModel_info";
    }

    @PostMapping("/save.do")
    public String save(RedirectAttributes model, AppModel appModel, String mode) {
        if (mode.equals("insert"))
            model.addAttribute("result", iAppModelServiceImpl.insertOne(appModel)>0);
        else
            model.addAttribute("result", iAppModelServiceImpl.updateOne(appModel));
        model.addAttribute("mode", mode);
        return "redirect:/manage/appModel/page.do";
    }

    @GetMapping("/delete.do")
    public String delete(RedirectAttributes model, int appModelId) {
        model.addAttribute("result", iAppModelServiceImpl.deleteById(appModelId));
        return "redirect:/manage/appModel/page.do";
    }

}

